package com.lz.sports.bean

import android.os.Parcel
import android.os.Parcelable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.JsonSerializationContext
import com.google.gson.JsonSerializer
import com.lz.helper.idcard.IDCardInfo
import com.lz.sports.ui.checkmodel.FeatureResult
import com.lz.sports.utils.DateUtils
import com.lz.sports.utils.UploadUtils
import com.lz.sports.utils.saveBitmapFile
import com.lz.sports.utils.saveNv21
import java.io.File
import java.lang.reflect.Type

//考试签到表
@Entity(tableName = "ExamSignIn")
class ExamSignIn() : Parcelable {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "examSignInId")
    var id: Int = 0

    //考生ID
    @ColumnInfo(name = "examineeId")
    var examineeId: Int = 0

    //考试计划ID
    @ColumnInfo(name = "examPlanId")
    var examPlanId: Int = 0

    //检录考区ID
    @ColumnInfo(name = "examAreaId")
    var examAreaId: Int = 0

    //检录的考点ID
    @ColumnInfo(name = "examSiteId")
    var examSiteId: Int = 0

    //签到时间
    @ColumnInfo(name = "signInTime")
    var signInTime: Long = 0


    //考试类型 9:JXN8-身份证-指纹-人脸（人照并人证)10:JXN9-身份证-人脸（人照并人证）-指纹
    @ColumnInfo(name = "examType")
    var examType: Int = 9

    //验证类型(通过)0人脸1指纹2手动
    @ColumnInfo(name = "verificationType")
    var verificationType: Int = -1

    //现场照片
    @ColumnInfo(name = "sceneImage")
    var sceneImage: String = ""

    //身份证照片
    @ColumnInfo(name = "cardImage")
    var cardImage: String = ""

    //报名照片
    @ColumnInfo(name = "photoImage")
    var photoImage: String = ""

    //身份证匹对结果
    @ColumnInfo(name = "idCardPhotoMate")
    var idCardPhotoMate: Float = 0f

    //报名照匹对结果
    @ColumnInfo(name = "examPhotoMate")
    var examPhotoMate: Float = 0f

    //指纹匹对结果
    @ColumnInfo(name = "fingerMate")
    var fingerMate: Float = 0f

    //验证是否通过：0未验证1通过2验证不通过3.存疑
    @ColumnInfo(name = "validateStatus")
    var validateStatus: Int = 0

    //现场照是否上传成功、
    @ColumnInfo(name = "scenePhotoUploadStatus")
    var scenePhotoUploadStatus = -1

    //身份证照否上传成功、
    @ColumnInfo(name = "idCardPhotoUploadStatus")
    var idCardPhotoUploadStatus = -1

    //报考照是否上传成功
    @ColumnInfo(name = "examPhotoUploadStatus")
    var examPhotoUploadStatus = -1

    //数据是否已经上传
    @ColumnInfo(name = "uploadStatus")
    var uploadStatus = -1

    fun signInTime(): String = DateUtils.format(signInTime)

    //身份证识别地址
    fun idCardImgFile(): File = File(UploadUtils.examSignInPath(), cardImage)

    //现场照地址
    fun sceneImgFile(): File = File(UploadUtils.examSignInPath(), sceneImage)

    //报名照地址
    fun examImgFile(): File = File(UploadUtils.examSignInPath(), photoImage)

    //当不要在主线程中构造
    @Ignore
    constructor(examinee: Examinee, idCardInfo: IDCardInfo) : this() {
        this.examineeId = examinee.id
        this.examPlanId = examinee.examPlanId
        this.examAreaId = examinee.examAreaId
        this.examSiteId = examinee.examSiteId
        this.signInTime = System.currentTimeMillis()
        this.examType = CommonModel.ID_CARD

        val file = File(UploadUtils.examSignInPath(), "card_${System.currentTimeMillis()}.jpg")
        idCardInfo.photoBitmap()?.also { saveBitmapFile(it, file.absolutePath, true) }
        cardImage = file.name
    }

    //当不要在主线程中构造
    @Ignore
    constructor(result: FeatureResult) : this() {
        this.examineeId = result.examinee.id
        this.examPlanId = result.examinee.examPlanId
        this.examAreaId = result.examinee.examAreaId
        this.examSiteId = result.examinee.examSiteId
        this.signInTime = System.currentTimeMillis()

        verificationType = 0
        examType = result.type
        val sceneFile = File(UploadUtils.examSignInPath(), "scene_${System.currentTimeMillis()}.jpg")
        saveNv21(result.nvData, result.w, result.h, sceneFile)
        sceneImage = sceneFile.name

        if (result.photoParams?.isSuccess() == true) {
            val cardFile = File(UploadUtils.examSignInPath(), "card_${System.currentTimeMillis()}.jpg")
            saveBitmapFile(result.photoParams.bitmap, cardFile, false)
            cardImage = cardFile.name
            idCardPhotoMate = result.photoParams.thresholdResult
        }

        if (result.examParams?.isSuccess() == true) {
            val examFile = File(UploadUtils.examSignInPath(), "exam_${System.currentTimeMillis()}.jpg")
            saveBitmapFile(result.examParams.bitmap, examFile, false)
            photoImage = examFile.name
            examPhotoMate = result.examParams.thresholdResult
        }
    }

    @Ignore
    constructor(parcel: Parcel) : this() {
        id = parcel.readInt()
        examineeId = parcel.readInt()
        examPlanId = parcel.readInt()
        examAreaId = parcel.readInt()
        examSiteId = parcel.readInt()
        signInTime = parcel.readLong()

        examType = parcel.readInt()
        verificationType = parcel.readInt()
        sceneImage = parcel.readString()!!
        cardImage = parcel.readString()!!
        photoImage = parcel.readString()!!
        idCardPhotoMate = parcel.readFloat()
        examPhotoMate = parcel.readFloat()
        fingerMate = parcel.readFloat()
        validateStatus = parcel.readInt()
        scenePhotoUploadStatus = parcel.readInt()
        idCardPhotoUploadStatus = parcel.readInt()
        examPhotoUploadStatus = parcel.readInt()
        uploadStatus = parcel.readInt()
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeInt(id)
        parcel.writeInt(examineeId)
        parcel.writeInt(examPlanId)
        parcel.writeInt(examAreaId)
        parcel.writeInt(examSiteId)
        parcel.writeLong(signInTime)

        parcel.writeInt(examType)
        parcel.writeInt(verificationType)
        parcel.writeString(sceneImage)
        parcel.writeString(cardImage)
        parcel.writeString(photoImage)
        parcel.writeFloat(idCardPhotoMate)
        parcel.writeFloat(examPhotoMate)
        parcel.writeFloat(fingerMate)
        parcel.writeInt(validateStatus)
        parcel.writeInt(scenePhotoUploadStatus)
        parcel.writeInt(idCardPhotoUploadStatus)
        parcel.writeInt(examPhotoUploadStatus)
        parcel.writeInt(uploadStatus)
    }

    override fun describeContents(): Int {
        return 0
    }

    override fun toString(): String {
        return "ExamSignIn(id=$id, examineeId=$examineeId, examPlanId=$examPlanId, examAreaId=$examAreaId, examSiteId=$examSiteId, signInTime=$signInTime, examType=$examType, verificationType=$verificationType, sceneImage='$sceneImage', cardImage='$cardImage', photoImage='$photoImage', idCardPhotoMate=$idCardPhotoMate, examPhotoMate=$examPhotoMate, fingerMate=$fingerMate, validateStatus=$validateStatus, scenePhotoUploadStatus=$scenePhotoUploadStatus, idCardPhotoUploadStatus=$idCardPhotoUploadStatus, examPhotoUploadStatus=$examPhotoUploadStatus, uploadStatus=$uploadStatus)"
    }

    companion object CREATOR : Parcelable.Creator<ExamSignIn> {
        override fun createFromParcel(parcel: Parcel): ExamSignIn {
            return ExamSignIn(parcel)
        }

        override fun newArray(size: Int): Array<ExamSignIn?> {
            return arrayOfNulls(size)
        }
    }

    class ExamSignSerializer : JsonSerializer<ExamSignIn> {
        override fun serialize(src: ExamSignIn, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement {
            val json = JsonObject()
            json.addProperty("studentId", src.examineeId)
            json.addProperty("examAreaId", src.examAreaId)
            json.addProperty("examPlanId", src.examPlanId)
            json.addProperty("examSiteId", src.examSiteId)

            json.addProperty("examType", src.examType)
            json.addProperty("cardImage", src.cardImage)
            json.addProperty("fingerprintPercentage", src.fingerMate)
            json.addProperty("personPhotoPercentage", src.idCardPhotoMate)
            json.addProperty("sceneImage", src.sceneImage)
            json.addProperty("validateStatus", src.validateStatus)
            json.addProperty("verificationPercentage", src.validateStatus)
            json.addProperty("verificationType", src.verificationType)
            json.addProperty("verificationTime", DateUtils.format2(src.signInTime))
            return json
        }
    }
}